package oop.ex3.filemanager;

import java.io.File;
import java.util.List;
import java.util.Scanner;

public class FMUserInterface {

	private final static String CMD_DIR = "DIR";
	private final static String CMD_DIR_SERVERS = "DIRSERVERS";
	private final static String CMD_QUIT = "QUIT";
	private final static String CMD_ADD_FILE = "ADD";
	private final static String CMD_REMOVE = "REMOVE";
	private static final String CMD_DIR_ALL_FILES = "DIRALLFILES";
	private static final String CMD_RENAME = "RENAME";
	private static final String CMD_FIRE_SERVERS = "FIRESERVERS";
	
	public static void listenToUser(MyFileManager fileManager) {

		Scanner scan = new Scanner(System.in);
		String cmd = scan.next();
		while (!cmd.equals(CMD_QUIT)) {
			//the DIR cmd:
			if (cmd.equals(CMD_DIR)) {
				doDir(fileManager);
			//the DIRSERVERS cmd:	
			}else if (cmd.equals(CMD_DIR_SERVERS)){
				doDirServers(fileManager);
			//the ADD cmd:	
			} else if (cmd.equals(CMD_ADD_FILE)) {
				//the name of the wanted file:
				String wantedFile = scan.next();
				doAddFile(fileManager, wantedFile);
			} else if (cmd.equals(CMD_REMOVE)){
				String fileToRemove = scan.next();
				doRemoveFile(fileManager, fileToRemove);
			} else if (cmd.equals(CMD_DIR_ALL_FILES)){
				doDirAllFiles(fileManager);
			} else if (cmd.equals(CMD_RENAME)){
				String oldName = scan.next();
				String newName = scan.next();
				doRenameFile(fileManager,oldName,newName);
			}else if(cmd.equals(CMD_FIRE_SERVERS)){
				doFireServers(fileManager);
			}
			cmd = scan.next();
		}
		doQuit(fileManager);
	}
	





	private static void doFireServers(MyFileManager fileManager) {
		fileManager.fireAllServers();
		
	}






	private static void doRenameFile(MyFileManager fileManager, String oldName, String newName) {
		String returnStatus = fileManager.rename(oldName, newName);
		System.out.println(returnStatus);
		
	}






	private static void doQuit(MyFileManager fileManager) {
		String returnStatus = fileManager.Quit();
		System.out.println(returnStatus);
	}



	private static void doRemoveFile(MyFileManager fileManager, String fileToRemove){
		String returnStatus = fileManager.remove(fileToRemove);
		System.out.println(returnStatus);
		
	}
	
	
	private static void doAddFile(MyFileManager fileManager, String wantedFile) {
		String returnStatus = fileManager.add(wantedFile);
		System.out.println(returnStatus);
	}

	private static void doDir(MyFileManager fileManager) {
		List<File> files = fileManager.listFiles();
		for (File file : files) {
			System.out.println(file.getName());
		}
	}
	
	private static void doDirAllFiles(MyFileManager fileManager) {
		List<String> allFiles = fileManager.getAllFilesInSystem();
		for(String fileName : allFiles){
			System.out.println(fileName);
		}
		
	}
	
	
	private static void doDirServers(MyFileManager filManager){
		List<NameServer> servers = filManager.listServers();
		for(NameServer nameServer : servers) {
			System.out.println(nameServer.getHost() + " " + Integer.toString((nameServer.getPort())));
		}
	}
}
